using System;
using System.Collections;
using System.Collections.Generic;
using Framework.Library.Core.Config;
using Framework.Library.Core.Data.DataEngine;
using Framework.Library.Data.DAL;
using Framework.Library.Data.IDAO;
using Framework.Library.Core.Utils.Database;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Linq;

namespace Framework.Library.Data.DAO.Oracle
{
    internal class PortalMenuManager : IPortalMenuManager
    {
        private Configurations cfg = new Configurations();
        #region IPortalMenuManager Members

        public List<PortalMenu> GetAll(int PortalID)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_MENU");
            builder.AddCondition("PORTALID", PortalID);
            builder.AddSortColumn("PRIORITY", SortOrder.Ascending);
            return GeneralConnection.ExecuteList<PortalMenu>(CommandType.Text, builder);
        }

        public void Delete(int _iD)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_MENU");
            builder.QueryType = QueryTypes.Delete;
            builder.AddCondition("ID", _iD);
            GeneralConnection.ExecuteNonQuery(CommandType.Text, builder);
        }

        public List<PortalMenu> GetTopMenuHierarchism(int MenuID)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_MENU");
            builder.AddCondition("PARENTID", MenuID);
            builder.AddSortColumn("PRIORITY", SortOrder.Ascending);
            return GeneralConnection.ExecuteList<PortalMenu>(CommandType.Text, builder);
        }

        public List<PortalMenu> GetByCategory(int PortalID, int MenuID)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_MENU");
            builder.AddCondition("PORTALID", PortalID);
            builder.AddCondition("PARENTID", MenuID);
            builder.AddSortColumn("PRIORITY", SortOrder.Ascending);

            return GeneralConnection.ExecuteList<PortalMenu>(CommandType.Text, builder);
        }

        public PortalMenu GetInfo(int PortalID, int ID)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_MENU");
            builder.AddCondition("PORTALID", PortalID);
            if (ID > 0)
                builder.AddCondition("ID ", ID);
            else
                builder.AddCondition("HOMEPAGE", 1);
            List<PortalMenu> tab = GeneralConnection.ExecuteList<PortalMenu>(CommandType.Text, builder);
            if (tab.Count > 0)
                return tab[0];
            return null;
        }

        public List<PortalMenu> GetAll(int PortalID, int UserId)
        {
            List<PortalMenu> retVal = new List<PortalMenu>();
            QueryBuilder builder = new QueryBuilder("PORTAL_MENU");
            builder.AddCondition("PORTALID", PortalID);
            var lstMenu = GeneralConnection.ExecuteList<PortalMenu>(CommandType.Text, builder);
            var roleInfo = new PortalRoleManager().GetRole(UserId, PortalID);
            if (roleInfo != null)
            {
                if(!string.IsNullOrEmpty(roleInfo.PERMISIONS))
                {
                    if (roleInfo.PERMISIONS != "0")
                    {
                        var lstPermission = roleInfo.PERMISIONS.Split(',');
                        if (lstPermission.Length > 0)
                        {
                            foreach (string valPer in lstPermission)
                            {
                                int Per = 0;
                                int.TryParse(valPer, out Per);
                                retVal.Add(lstMenu.Find(x => x.ID == Per));
                            }
                        }
                    }
                    else
                        retVal = lstMenu;
                }
            }
            return retVal;
        }
        #endregion
    }
}